package com.myComsole.chat;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.json.JSONUtil;
import com.myComsole.chat.model.ChatSessionModel;
import com.myComsole.chat.model.Conversations;
import com.myComsole.chat.model.Message;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @Date 2025/8/25 18:19
 * @Author huangfaxiang
 */
public class ChatgptHandle {
    private  String sourcePath = "D:\\chatgpt对话\\conversations.json";
    private  String baseResultPath = "D:\\chatgpt对话\\";
    private Conversations conversations;

    public static ChatgptHandle build(String sourcePath){
        ChatgptHandle dsHandle = new ChatgptHandle();
        dsHandle.sourcePath=sourcePath;
        dsHandle.openFile();
        return dsHandle;
    }
    public static ChatgptHandle buildDefault(){
        ChatgptHandle dsHandle = new ChatgptHandle();
        dsHandle.openFile();
        return dsHandle;
    }

    private void openFile() {
        File file = new File(sourcePath);
        String text = FileUtil.readString(file, StandardCharsets.UTF_8);
        //转换中文字符编码
        String chineseText = UnicodeUtil.toString(text);
        conversations = JSONUtil.toBean(chineseText, Conversations.class);
    }

    public List<String> getAllTitle() {
        List<ChatSessionModel> sessions = conversations.getSessions();
        List<String> titles = sessions.stream().map(ChatSessionModel::getTitle).collect(Collectors.toList());
        return titles;
    }
    public void exportUserContentTxtByTitle(String title){
        ChatSessionModel chatSessionModel = selectByTitle(title);
        Map<String, Message> mapping = chatSessionModel.getMapping();
        //获取所有用户对话
        List<String> userContents = new ArrayList<>();
        for (Message message : mapping.values()) {
            if (message.isUserContent()){
                userContents.add(message.getMsgContent());
            }
        }
        String resultText = dealMyContents(userContents);
        String exportTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String resultPath = baseResultPath + title + exportTime + ".txt";
        if (FileUtil.exist(resultPath)) {
            FileUtil.del(resultPath);
        }
        FileUtil.writeString(resultText,resultPath , StandardCharsets.UTF_8);
    }
    private static String dealMyContents(List<String> myContents){
        StringBuilder content = new StringBuilder();
        for (String myContent : myContents) {
            myContent = myContent.trim().replace("\r\n","。");
            myContent = myContent.trim().replace("\n","。");
            myContent = myContent.trim().replace("\t","。");
            content.append(myContent).append("。");
        }
        return content.append(".").toString();
    }

    private ChatSessionModel selectByTitle(String title){
        List<ChatSessionModel> sessions = conversations.getSessions();
        for (ChatSessionModel session : sessions) {
            if (session.getTitle().contains(title)){
                return session;
            }
        }
        throw new RuntimeException("标题错误，无包含此标题的对话");
    }
}
